<form class="layui-form" id="checkPaper">
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
            <button class="layui-btn layui-btn-primary" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
<script>
    layui.use(['form','index',  'config', 'admin', 'layer'], function () {
        var admin=layui.admin;
        var layer=layui.layer;
        var form=layui.form;
        let quesids=[];
        //试卷数据
        var tempData = admin.getTempData("paper_data");
        admin.req("api-exam/exam/findResult",{expaid:tempData.expaid,userid:tempData.userid},function (res) {
            let answerData=res.data;
            generatorHtml(answerData);
        },"get");
        //生成html
        function generatorHtml(data){
            let html2 = notChoice(data);
            $("#checkPaper").html(html2);
            form.render();
        }
        //非选择题html
        function notChoice(data) {
            let flag=true;
            let html='<div style="font-weight: bold">问答题</div>\n';
            let num=1;
            for (let i = 0; i < data.length; i++) {
                if(data[i].tpname!="选择题"){
                    quesids.push(data[i].quesid);
                    html += '<div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">'+num+'、('+data[i].score+'分) </label>\n' +
                        '    <div class="layui-input-block" contenteditable="true">\n' +
                        '        <textarea  class="layui-textarea"  autoHeight="true" readonly>'+data[i].qtitle+'</textarea>\n' +
                        '    </div>\n' +
                        '</div>\n' +
                        '<div class="layui-form-item ">\n' +
                        '    <label class="layui-form-label">答案</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <textarea  class="layui-textarea "  autoHeight="true" readonly>'+data[i].answer+'</textarea>\n' +
                        '    </div>\n' +
                        '</div>'+
                        '<div class="layui-form-item layui-bg-gray">\n' +
                        '    <label class="layui-form-label">参考答案</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <textarea  class="layui-textarea layui-bg-gray"  autoHeight="true" readonly>'+data[i].standard+'</textarea>\n' +
                        '    </div>\n' +
                        '</div>'+
                        '<div class="layui-form-item " >\n' +
                        '    <label class="layui-form-label"><span class="layui-badge-dot"></span>判题</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <input  type="radio" name="result'+data[i].quesid+'" class="layui-input" value="1" title="正确"  >\n' +
                        '        <input  type="radio" name="result'+data[i].quesid+'" class="layui-input" value="2" title="错误"  >\n' +
                        '        <input  type="radio" name="result'+data[i].quesid+'" class="layui-input" value="3" title="半对"  >\n' +
                        '    </div>\n' +
                        '</div>';
                    num++;
                    flag=false;
                }
            }
            html+=' <div class="layui-form-item model-form-footer">\n' +
                '        <div class="layui-input-block">\n' +
                '            <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>\n' +
                '            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>\n' +
                '        </div>\n' +
                '    </div>';
            if(flag){
                html='';
            }
            return html;
        }

        form.on("submit(saveBtn)",function (data) {
            let formData=data.field;
            let dataLen = Object.getOwnPropertyNames(formData).length;
            console.log(quesids,53)
            console.log(dataLen)
            if(dataLen!=quesids.length){
                layer.msg("还未批改完哦");
                return false;
            }

            let checks=[];
            //判卷结果
            for (let j = 0; j < quesids.length; j++) {
                let check = $("input[type='radio'][name='result"+quesids[j]+"']:checked").val();
                checks.push(check)
            }
            console.log(quesids,23424);
            console.log(checks,24);
            let params=[];
            for (let i = 0; i <quesids.length; i++) {
                let param={
                    expaid:tempData.expaid,
                    userid:tempData.userid,
                    quesid:quesids[i],
                    result:checks[i]
                };
                params.push(param);
            }
            admin.req("api-exam/exam/updateExam",JSON.stringify(params),function (res) {
                layer.msg(res.msg())
            },"post");
            return false;
        })
    });


    /**
     * textarea高度自适应
     */
    $(function(){
        $.fn.autoHeight = function(){
            function autoHeight(elem){
                elem.style.height = 'auto';
                elem.scrollTop = 0; //防抖动
                elem.style.height = elem.scrollHeight +5+ 'px';
            }
            this.each(function(){
                autoHeight(this);
                $(this).on('keyup', function(){
                    autoHeight(this);
                });
            });
        };
        $('textarea[autoHeight]').autoHeight();
    })
</script>